﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;
namespace Congducsan
{
    public partial class FrmMuongTl : Form
    {
        public FrmMuongTl()
        {
            InitializeComponent();
        }

        private void tabControl2_Click(object sender, EventArgs e)
        {
            if (tabControl2.SelectedTab == tabControl2.TabPages[0])
            {
                pictureBox1.Image = Congducsan.Properties.Resources.MuongTld;
            }
            else
            {
                pictureBox1.Image = Congducsan.Properties.Resources.MuongTlm;
            }
        }

        private void FrmMuongTl_FormClosing(object sender, FormClosingEventArgs e)
        {
            DradApplication.DradApp.FormMain.Show();
        }

        private void FrmMuongTl_Resize(object sender, EventArgs e)
        {
            this.Size = new Size(436, 360);
        }

        private void CmdOk_Click(object sender, EventArgs e)
        {
            PrMuongTl prMuongTl = new PrMuongTl();
            prMuongTl.Show();
            this.Hide();
        }

        private void FrmMuongTl_Load(object sender, EventArgs e)
        {
            loadSettingReg();
        }
         // lưu setting vào registry
        private void saveSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            string Values = "";
            //Tuong dau
            Values = txtTDau_DayMTr.Text;//0
            Values = Values +"\t"+ txtTDau_DayMSau.Text;//1
            Values = Values +"\t"+ txtTDau_Day.Text;//2
            Values = Values +"\t"+ txtTDau_doc.Text;//3
            Values = Values +"\t"+ 0;//4
            Values = Values +"\t"+ txtTDau_mrongS.Text;//5
            //Muong thuy loi
            Values = Values +"\t"+ txtDaymaid1.Text;//6
            Values = Values +"\t"+ txtDocmaim1.Text;//7
            Values = Values +"\t"+ txtDaylongH.Text;//8
            Values = Values +"\t"+ txtVaimuongb1.Text;//9
            Values = Values +"\t"+ txtLopdem.Text;//10
            regKey.SetValue("KtMuongTl", Values);
            regKey.Close();
        }
        // load settings từ registry vào form
        private void loadSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            RegistryKey testKey = regKey.OpenSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            if (testKey != null)
            {
                regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
                string str = regKey.GetValue("KtMuongTl").ToString();
                string[] STR = str.Split('\t');
                int i = 0;
                //Tuong dau
                txtTDau_DayMTr.Text = STR[i++] ;//0
                txtTDau_DayMSau.Text = STR[i++];//1
                txtTDau_Day.Text = STR[i++];//2
                txtTDau_doc.Text = STR[i++];//3
                i++;
                //0 ;//4
                txtTDau_mrongS.Text = STR[i++];//5
                //Muong thuy loi
                txtDaymaid1.Text = STR[i++];//6
                txtDocmaim1.Text = STR[i++];//7
                txtDaylongH.Text = STR[i++];//8
                txtVaimuongb1.Text = STR[i++];//9
                txtLopdem.Text = STR[i++];//10
                regKey.Close();
            }
        }
        // Save setting vào file
        private void saveSettingFile()
        {
            SaveFileDialog svDialog = new SaveFileDialog();
            svDialog.Filter = "Data files (*.muong)|*.muong";
            svDialog.InitialDirectory = "C://";
            svDialog.RestoreDirectory = true;
            string filename = "";
            if (svDialog.ShowDialog() == DialogResult.OK)
            {
                filename = svDialog.FileName;
                string Str;
                //Tuong dau
                Str = "------TUONG DAU------"+"\r\n";
                Str = Str + "Tuong dau:" +"\t"+ "True"+"\r\n";
                Str = Str + "Cao tuong H1:" +"\t"+ txtTDau_cao .Text+"\r\n";
                Str = Str + "Day mong truoc H2:" +"\t"+ txtTDau_DayMTr .Text+"\r\n";
                Str = Str + "Day mong sau H3:" +"\t"+ txtTDau_DayMSau .Text+"\r\n";
                Str = Str + "Day dem lot H4:" +"\t"+ txtLopdem .Text+"\r\n";
                Str = Str + "Day dinh tuong b1:" +"\t"+ txtTDau_Day .Text+"\r\n";
                Str = Str + "Mo rong mong b2:" +"\t"+ txtTDau_mrongS .Text+"\r\n";
                Str = Str + "Rong tuong b3:" +"\t"+ txtTDau_rong .Text+"\r\n";
                Str = Str + "Doc lung tuong i:" +"\t"+ txtTDau_doc .Text+"\r\n";
                //Muong
                Str = Str + "------MUONG------" +"\r\n";
                Str = Str + "Cao muong H1:" +"\t"+ txtCaomuongH1 .Text+"\r\n";
                Str = Str + "Cao muong H2:" +"\t"+ txtCaomuongH2 .Text+"\r\n";
                Str = Str + "Day long H3:" +"\t"+ txtDaylongH .Text+"\r\n";
                Str = Str + "Vai muong b1:" +"\t"+ txtVaimuongb1 .Text+"\r\n";
                Str = Str + "Vai muong b2:" +"\t"+ txtVaimuongb2 .Text+"\r\n";
                Str = Str + "Day mai d1:" +"\t"+ txtDaymaid1 .Text+"\r\n";
                Str = Str + "Day mai d2:" +"\t"+ txtDaymaid2 .Text+"\r\n";
                Str = Str + "Doc mai m1:" +"\t"+ txtDocmaim1 .Text+"\r\n";
                Str = Str + "Doc mai m2:" +"\t"+ txtDocmaim2 .Text+"\r\n";
                Str = Str + "Rong long B:" +"\t"+ txtRonglongB .Text+"\r\n";
                Str = Str + "Dai muong L:" +"\t"+ txtDaimuong.Text;
                //Ghi file dữ liệu
                FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(Str);
                sw.Close();
                fs.Close();
                MessageBox.Show("OK", "Thông báo");

            }
        }
         // load setting từ file vào form
        private void loadSettingFile()
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "Data files (*.muong)|*.muong";
            openDialog.InitialDirectory = @"C:\";
            openDialog.FilterIndex = 2;
            openDialog.RestoreDirectory = true;
            openDialog.Multiselect = false;
            string filename;
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                filename = openDialog.FileName;
                FileStream fs = new FileStream(filename, FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string str = sr.ReadToEnd();
                fs.Close();
                fs.Close();
                string[] Values = str.Split('\n');
                int i = 1;
                //Tuong dau
               // ChcTd
                i++;
                txtTDau_cao .Text= Values[i].Split('\t')[1]; i++;
                txtTDau_DayMTr .Text= Values[i].Split('\t')[1]; i++;
                txtTDau_DayMSau .Text= Values[i].Split('\t')[1]; i++;
                txtLopdem .Text= Values[i].Split('\t')[1]; i++;
                txtTDau_Day .Text= Values[i].Split('\t')[1]; i++;
                txtTDau_mrongS .Text= Values[i].Split('\t')[1]; i++;
                txtTDau_rong .Text= Values[i].Split('\t')[1]; i++;
                txtTDau_doc .Text= Values[i].Split('\t')[1]; i++;
                //Muong
                i = i + 1;
                txtCaomuongH1 .Text= Values[i].Split('\t')[1]; i++;
                txtCaomuongH2 .Text= Values[i].Split('\t')[1]; i++;
                txtDaylongH .Text= Values[i].Split('\t')[1]; i++;
                txtVaimuongb1 .Text= Values[i].Split('\t')[1]; i++;
                txtVaimuongb2 .Text= Values[i].Split('\t')[1]; i++;
                txtDaymaid1 .Text= Values[i].Split('\t')[1]; i++;
                txtDaymaid2 .Text= Values[i].Split('\t')[1]; i++;
                txtDocmaim1 .Text= Values[i].Split('\t')[1]; i++;
                txtDocmaim2 .Text= Values[i].Split('\t')[1]; i++;
                txtRonglongB .Text= Values[i].Split('\t')[1]; i++;
                txtDaimuong.Text = Values[i].Split('\t')[1]; i++;
                MessageBox.Show("OK", "Thông báo");
            }
        }

        private void SaveData_Click(object sender, EventArgs e)
        {
            saveSettingFile();
        }

        private void LoadData_Click(object sender, EventArgs e)
        {
            loadSettingFile();
        }

        private void FrmMuongTl_FormClosed(object sender, FormClosedEventArgs e)
        {
            saveSettingReg();
        }

        private void cmdThietlap_Click(object sender, EventArgs e)
        {
            FrmTuychon frmTuychon = new FrmTuychon();
            frmTuychon.Show();
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
